package cn.codesea.housework_user.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGALocalImageSize;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.HomeRcvAdapter;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickActivityListener;
import cn.codesea.housework_user.entity.Activity;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.ActivityService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Tab1HomeFragment extends Fragment implements BGABanner.Adapter<ImageView, String>{

    @BindView(R.id.banner)
    BGABanner banner;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    List<Activity> acts = new ArrayList<>();
    private HomeRcvAdapter mHomeRcvAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    @BindView(R.id.more_ll)
    LinearLayout more_ll;

    @BindView(R.id.lifeCare_ll)
    LinearLayout lifeCare_ll;// 生活料理
    @BindView(R.id.kfCare_ll)
    LinearLayout kfCare_ll;// 康复护理
    @BindView(R.id.counseling_ll)
    LinearLayout counseling_ll;// 心里咨询
    @BindView(R.id.care_ll)
    LinearLayout care_ll;// 安心托养
    @BindView(R.id.entertainment_ll)
    LinearLayout entertainment_ll;// 娱乐健身

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1_home,container,false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            initData();
        }
    }

    private void init(){
//        initData();
        initView();
    }

    private void initView(){
        BGALocalImageSize localImageSize = new BGALocalImageSize(720, 1280, 320, 640);

        banner.setData(localImageSize, ImageView.ScaleType.CENTER_CROP,
                R.drawable.banner1,
                R.drawable.banner2,
                R.drawable.banner3,
                R.drawable.banner4);

        mHomeRcvAdapter = new HomeRcvAdapter(getActivity(),acts);
        mHomeRcvAdapter.setRecyclerItemClickListener(new OnRecyclerItemClickActivityListener() {
            @Override
            public void onItemClick(int Position, List<Activity> list) {
                startActivity(new Intent(getActivity(),AcActivity.class).putExtra("act",acts.get(Position)));
            }
        });
        mLinearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);

        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(mHomeRcvAdapter);

    }

    private void initData() {
        ActivityService activityService = ServiceGenerator.createService(ActivityService.class);
        Call<Result> call = activityService.getActs();
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    private void resultProcess(Result result){
        Type type = new TypeToken<List<Activity>>(){}.getType();
        acts.clear();
//        acts.addAll((List<Activity>)new Gson().fromJson(String.valueOf(result.getRes()),type));
        // 这里只显示4个
        List<Activity> list = new Gson().fromJson(String.valueOf(result.getRes()),type);
        for (int i=0;i<list.size();i++){
            if (i>3){
                break;
            }
            acts.add(list.get(i));
        }
        mHomeRcvAdapter.notifyDataSetChanged();
    }

    @Override
    public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
        Glide.with(itemView.getContext())
                .load(model)
                .apply(new RequestOptions().placeholder(R.drawable.holder).error(R.drawable.holder).dontAnimate().centerCrop())
                .into(itemView);
    }

    @OnClick(R.id.more_ll)
    public void more_ll(){
        startActivity(new Intent(getActivity(),AcListActivity.class));
    }

    @OnClick(R.id.lifeCare_ll)
    public void lifeCare_ll(){
        startActivity(new Intent(getActivity(),ServiceListActivity.class).putExtra("title","生活料理").putExtra("service_id",1));
    }

    @OnClick(R.id.kfCare_ll)
    public void kfCare_ll(){
        startActivity(new Intent(getActivity(),ServiceListActivity.class).putExtra("title","康复护理").putExtra("service_id",2));
    }

    @OnClick(R.id.counseling_ll)
    public void counseling_ll(){
        startActivity(new Intent(getActivity(),ServiceListActivity.class).putExtra("title","心里咨询").putExtra("service_id",3));
    }

    @OnClick(R.id.care_ll)
    public void care_ll(){
        startActivity(new Intent(getActivity(),ServiceListActivity.class).putExtra("title","安心托养").putExtra("service_id",4));
    }

    @OnClick(R.id.entertainment_ll)
    public void entertainment_ll(){
        startActivity(new Intent(getActivity(),ServiceListActivity.class).putExtra("title","娱乐健身").putExtra("service_id",5));
    }

}
